package com.ehotting.edsta.diagnosisPredict.controller;

import com.ehotting.eaf.core.domain.R;
import com.ehotting.eaf.core.web.domain.diagnosisPredict.PageDto;
import com.ehotting.edsta.diagnosisPredict.domain.dto.ConnectInfoDto;
import com.ehotting.edsta.diagnosisPredict.domain.dto.SQLRequestDto;
import com.ehotting.edsta.diagnosisPredict.feign.DatabaseInfoFeignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 建模数据管理模块
 */
@RestController
@RequestMapping("/")
@Api(tags = "DatabaseInfoController",description = "建模数据管理模块")
public class DatabaseInfoController {

    @Autowired
    private DatabaseInfoFeignService databaseInfoFeignService;

    /**
     * 新增数据库连接信息
     * @param dto
     * @return
     */
    @PostMapping(value = "/insert-database")
    @ApiOperation("新增数据库连接信息")
    public R<String> insertDatabase(@RequestBody ConnectInfoDto dto) {
        return R.convert(databaseInfoFeignService.insertDatabase(dto));
    }

    /**
     * 更新连接账号密码
     * @param id
     * @param password
     * @param user
     * @return
     */
    @GetMapping(value = "/update-database")
    @ApiOperation("更新连接账号密码")
    public R<String> updateDatabase(@RequestParam("id") Long id,
                                    @RequestParam("password") String password,
                                    @RequestParam("user") String user) {
        return R.convert(databaseInfoFeignService.updateDatabase(id,password,user));
    }

    /**
     * 查询历史数据库连接信息
     * @param creater
     * @return
     */
    @GetMapping(value = "/history")
    @ApiOperation("查询历史数据库连接信息")
    public R<List<ConnectInfoDto>> history(@RequestParam("creater") String creater) {
        return R.convert(databaseInfoFeignService.history(creater));
    }

    /**
     * 获取连接素材库所有素材表
     * @param id
     * @param likeStr
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/get-table-list/{id}")
    @ApiOperation("获取连接素材库所有素材表")
    public R<PageDto<String>> getTableList(@PathVariable("id")Long id,
                                           @RequestParam("likeStr") String likeStr,
                                           @RequestParam("page")Integer page,
                                           @RequestParam("pageSize")Integer pageSize) {
        return R.convert(databaseInfoFeignService.getTableList(id,likeStr,page,pageSize));
    }

    /**
     * 根据表格获取预览前10行数据
     * @param id
     * @param tableName
     * @return
     */
    @GetMapping(value = "/get-by-table")
    @ApiOperation("根据表格获取预览前10行数据")
    public R<Map<String, Object>> getByTable(@RequestParam("id")Long id, @RequestParam("tableName") String tableName) {
        return R.convert(databaseInfoFeignService.getByTable(id,tableName));
    }

    /**
     * 根据sql获取预览前10行数据
     * @param requestDto
     * @return
     */
    @PostMapping(value = "/get-by-sql")
    @ApiOperation("根据sql获取预览前10行数据")
    public R<Map<String, Object>> getBySql(@RequestBody SQLRequestDto requestDto) {
        return R.convert(databaseInfoFeignService.getBySql(requestDto));
    }

}